﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure.StorageClient;
using WebRole1.Search.Query;

namespace Microsoft.WindowsAzure.Search
{
    public class DocInfoParser
    {
        public static String[] Parse(CloudBlobContainer indexContainer)
        {
            // get document path
            CloudBlob docInfoBloba = indexContainer.GetBlobReference("docInfoa");
            CloudBlob docInfoBlobg = indexContainer.GetBlobReference("docInfog");
            CloudBlob docInfoBlobq = indexContainer.GetBlobReference("docInfoq");

            String docInfoa = docInfoBloba.DownloadText();
            String docInfog = docInfoBlobg.DownloadText();
            String docInfoq = docInfoBlobq.DownloadText();

            String[] docLinea = Tokenizer.LineTokenize(docInfoa);
            String[] docLineg = Tokenizer.LineTokenize(docInfog);
            String[] docLineq = Tokenizer.LineTokenize(docInfoq);

            return Merge(docLinea, docLineg, docLineq);
        }

        private static string[] Merge(string[] docLinea, string[] docLineg, string[] docLineq)
        {
            return docLinea.Union(docLineg).Union(docLineq).ToArray();
        }

        public static double[] GetDocLength(String[] docLines)
        {
            int docNum = Convert.ToInt32(docLines.Length);

            // document length (normalization factor)
            double[] lengths = new double[docNum];

            int d = 0;
            for (int i = 0; i < docLines.Length; i++)
            {
                String[] docMapLine = Tokenizer.CommaTokenize(docLines[i]);
                lengths[d++] = Math.Sqrt(Convert.ToDouble(docMapLine[1]));
            }

            return lengths;
        }
    }
}